<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib uri="/tags/core" prefix="c"%>
<%@ taglib uri="/tags/fmt" prefix="fmt"%>
<script type="text/javascript" src="js/utils.js"></script>
<LINK REL="StyleSheet" HREF="css/principal.css" TYPE="text/css" />
<script type="text/javascript" src="js/calendar/PopupWindow.js"></script>
<script type="text/javascript" src="js/calendar/CalendarPopup.js"></script>
<script type="text/javascript" src="js/calendar/AnchorPosition.js"></script>
<script type="text/javascript" src="js/calendar/date.js"></script>
<script type="text/javascript">
		var calFechaNacimiento = new CalendarPopup("div_fechaNacimiento");
    	calFechaNacimiento.showNavigationDropdowns();
   		calFechaNacimiento.setCssPrefix("TEST");
    	calFechaNacimiento.setYearSelectStartOffset(80);
   		
   		function openCalendar(dateField){
			calFechaNacimiento.select(dateField,'img_purchasedate','dd/MM/yyyy'); 
			return false;
   		}

		function submitForm(action){			
	    	var form = document.getElementById('mainForm');	    	
	    	form.action = action;
	    	form.submit();	    		    	   
		}
		
		function validate(){
        var errors = "";
        var customerCode = document.getElementById('futurePurchaseByCustomerView.customerCode').value;
        var dealNumber = document.getElementById('futurePurchaseByCustomerView.dealNumber').value;
        var price = document.getElementById('futurePurchaseByCustomerView.price').value;
        var reference = document.getElementById('futurePurchaseByCustomerView.reference').value;
        var date = document.getElementById('date').value;
                                                                    
        if(customerCode == "" || parseInt( customerCode ) == 0) {
        	errors += ' <bean:message key="lable.plants.validation.code"/>\n';		        	
        }	
        if(dealNumber == "" || parseInt( dealNumber ) == 0) {
        	errors += ' <bean:message key="lable.corn.error.input.quantity"/>\n';
        }
        if(reference == "" || parseInt( reference ) == 0) {
        	errors += ' <bean:message key="lable.corn.error.input.reference"/>\n';
        }
        if(price == "" || parseInt( price ) == 0) {
        	errors += ' <bean:message key="lable.corn.error.input.unitprice"/>\n';
        }
        if(trim(date) == ""){                
        	errors += ' <bean:message key="error.message.futurepurchase.wrong.empy"/>\n';			  
        }else if(!isValidDate(trim(date))){
        	errors += ' <bean:message key="error.message.futurepurchase.wrong.date"/>\n';
        }
        if( errors == "" ){
            return true;
        }else{
            alert( '<bean:message key="label.common.errors"/> : \n' + errors );            
			return false;
        }
	}
	
	function leaveThisForm(forma, productId){
		forma.customerCode.value = document.getElementById('futurePurchaseByCustomerView.customerCode').value;
		forma.customerDescription.value = document.getElementById('futurePurchaseByCustomerView.description').value;
		forma.contractType.value = document.getElementById('futurePurchaseByCustomerView.dealType').value;
		forma.contratNumbers.value = document.getElementById('futurePurchaseByCustomerView.dealNumber').value;
		forma.contratTotalNumbers.value = document.getElementById('futurePurchaseByCustomerView.dealTotalNumber').value;
		forma.contratLastNumbers.value = document.getElementById('futurePurchaseByCustomerView.dealLastNumber').value;
		forma.accountId.value = document.getElementById('futurePurchaseByCustomerView.accountPeriod').value;
		forma.applicationId.value = document.getElementById('futurePurchaseByCustomerView.applicationPeriod').value;
		forma.reference.value = document.getElementById('futurePurchaseByCustomerView.reference').value;
		forma.price.value = document.getElementById('futurePurchaseByCustomerView.price').value;
		forma.purchaseDate.value = document.getElementById('date').value;
		forma.futurePurchaseId.value = document.getElementById('futurePurchaseByCustomerView.futurePurchaseId').value;
		forma.futureDistributionId.value = document.getElementById('futurePurchaseByCustomerView.futureDistributionId').value;
		if( productId != undefined ){
			forma.productId.value = productId;
		}
		forma.submit();
	}
	function selectApplicationPeriod(field){
		var applicationFuturePeriodId = document.getElementById("futurePurchaseByCustomerView.applicationPeriod");
		applicationFuturePeriodId.options[field.selectedIndex].selected = true;
	}
</script>
<html:form action="/customerEditFutureDeleteProduct">
	<input type="hidden" name="customerCode"/>
	<input type="hidden" name="customerDescription"/>
	<input type="hidden" name="contractType"/>
	<input type="hidden" name="contratNumbers"/>
	<input type="hidden" name="contratLastNumbers"/>
	<input type="hidden" name="contratTotalNumbers"/>
	<input type="hidden" name="accountId"/>
	<input type="hidden" name="applicationId"/>
	<input type="hidden" name="reference"/>
	<input type="hidden" name="price"/>
	<input type="hidden" name="purchaseDate"/>
	<input type="hidden" name="productId"/>
	<input type="hidden" name="futurePurchaseId"/>
	<input type="hidden" name="futureDistributionId"/>
</html:form>
<html:form action="/customerFuturePurchaseEditProduct">
	<input type="hidden" name="customerCode"/>
	<input type="hidden" name="customerDescription"/>
	<input type="hidden" name="contractType"/>
	<input type="hidden" name="contratNumbers"/>
	<input type="hidden" name="contratLastNumbers"/>
	<input type="hidden" name="contratTotalNumbers"/>	
	<input type="hidden" name="accountId"/>
	<input type="hidden" name="applicationId"/>
	<input type="hidden" name="reference"/>
	<input type="hidden" name="price"/>
	<input type="hidden" name="purchaseDate"/>
	<input type="hidden" name="futurePurchaseId"/>
	<input type="hidden" name="futureDistributionId"/>
</html:form>
<html:form action="/editClientDetail.do" styleId="mainForm" onsubmit="return validate();">
	<html:hidden name="CustomerFuturePurchaseForm" property="futurePurchaseByCustomerView.futurePurchaseId" styleId="futurePurchaseByCustomerView.futurePurchaseId"/>
	<html:hidden name="CustomerFuturePurchaseForm" property="futurePurchaseByCustomerView.futureDistributionId" styleId="futurePurchaseByCustomerView.futureDistributionId"/>
<DIV ID="div_fechaNacimiento"
	STYLE="position:absolute;visibility:hidden;background-color:white;"></DIV>
	
	<table align="center" width="40%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
		<tr>
			<th colspan="2" align="center" class="titulocolumna"> <bean:message key="label.versions.edit.customerDetail" /> </th>			 
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="label.plants.add.column.code"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> 
				<html:text name="CustomerFuturePurchaseForm" styleId="futurePurchaseByCustomerView.customerCode" property="futurePurchaseByCustomerView.customerCode" maxlength="3" size="10"  styleClass="campo_texto" onfocus="this.select();" readonly="true"/>				
			</td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="label.plants.add.column.description"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> <html:text name="CustomerFuturePurchaseForm" styleId="futurePurchaseByCustomerView.description" property="futurePurchaseByCustomerView.description" styleClass="campo_texto" maxlength="255" size="30" readonly="true" /> </td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="label.customerfuturepurchase.deal.type"/> </td>
			<td class="etiqueta_campo" style="text-align: left;">
				<html:select name="CustomerFuturePurchaseForm" styleId="futurePurchaseByCustomerView.dealType" property="futurePurchaseByCustomerView.dealType" styleClass="campo_texto">
					<html:option value="F"> <bean:message key="label.futuretype.F" /> </html:option>
					<html:option value="D"> <bean:message key="label.futuretype.D" /> </html:option>
				</html:select> 
			</td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="label.customerfuturepurchase.deal.number"/> </td>
			<td class="etiqueta_campo" style="text-align: left;">
				<html:hidden name="CustomerFuturePurchaseForm" property="futurePurchaseByCustomerView.dealTotalNumber" styleId="futurePurchaseByCustomerView.dealTotalNumber"/>
				<html:hidden name="CustomerFuturePurchaseForm" property="futurePurchaseByCustomerView.dealLastNumber" styleId="futurePurchaseByCustomerView.dealLastNumber"/>
				<html:text name="CustomerFuturePurchaseForm" property="futurePurchaseByCustomerView.dealNumber" styleId="futurePurchaseByCustomerView.dealNumber" styleClass="columnanumero" onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,0,false);"/>
			</td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="message.futurepurchase.periodaccount"/> </td>
			<td class="etiqueta_campo" style="text-align: left;">			
				<html:select name="CustomerFuturePurchaseForm" property="futurePurchaseByCustomerView.accountPeriod" styleId="futurePurchaseByCustomerView.accountPeriod" onchange="selectApplicationPeriod(this);" styleClass="campo_texto">
					<logic:notEmpty name="periodAccount">
						<html:optionsCollection name="periodAccount" value="periodId" label="periodName"/>
					</logic:notEmpty>
				</html:select> 
			</td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="message.futurepurchase.periodapplication"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> 
			  <html:select name="CustomerFuturePurchaseForm" property="futurePurchaseByCustomerView.applicationPeriod" styleId="futurePurchaseByCustomerView.applicationPeriod" styleClass="campo_texto">
				<logic:notEmpty name="periodAccount">
					<html:optionsCollection name="periodAccount" value="periodId" label="periodName"/>
				</logic:notEmpty>
			  </html:select> 
			</td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="message.futurepurchase.reference"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> 
				<html:text name="CustomerFuturePurchaseForm" property="futurePurchaseByCustomerView.reference" styleId="futurePurchaseByCustomerView.reference" styleClass="campo_texto" onfocus="this.select();"/>
			</td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="label.futurepurchase.query.price"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> 
				<html:text name="CustomerFuturePurchaseForm" property="futurePurchaseByCustomerView.price" styleId="futurePurchaseByCustomerView.price" styleClass="columnanumero" onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,4,false);"/>
			</td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="message.futurepurchase.cornorigin"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> 
				<html:select name="CustomerFuturePurchaseForm" property="futurePurchaseByCustomerView.cornOrigin" styleId="futurePurchaseByCustomerView.cornOrigin"styleClass="campo_texto" style="width:110px">
					<html:option value="I"><bean:message key="message.futurepurchase.cornorigin.imported"/></html:option>
					<html:option value="N"><bean:message key="message.futurepurchase.cornorigin.national"/></html:option>
				</html:select>
			</td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="message.futurepurchase.purchase.date.single"/> </td>
			<td class="etiqueta_campo" style="text-align: left;">
			<html:text name="CustomerFuturePurchaseForm" property="futurePurchaseByCustomerView.date"  styleClass="campo_texto" styleId="date" style="width:110px" />
				<a href="javascript:var obj_dummy = openCalendar(document.getElementById('date'));"
					class="pointerhand" NAME="img_purchasedate" ID="img_purchasedate">
					<img id="fechatrigger" src="images/calendar.gif" border="0" />
				</a> 
			</td>			 
		</tr>	
	</table>
	<br />
	<br />
	<table align="center" width="400" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
	<tr>
		<td colspan="5" align="center" class="titulo_lista_interna"><B><bean:message key="message.futurepurchase.products"/></B></td>
	</tr>
	<tr>
		<td class="titulocolumna" align="center" width="20">
			<a href="#>" onclick="leaveThisForm(FuturePurchaseForm);"><img alt="<bean:message key="label.common.add"/> <bean:message key="message.futurepurchase.products"/>" src="images/nuevo.gif" border="0"></a>
			<html:link action="/customerFuturePurchaseEditProduct.do"></html:link>						
		</td>
		
		<td class="titulocolumna" align="center" width="70">
			<bean:message key="label.transports.query.code"/> 
		</td>
		
		<td class="titulocolumna" align="center" width="70">
			<bean:message key="message.futurepurchase.customer.description"/>
		</td>
		
		<td class="titulocolumna" align="center" width="70">
			<bean:message key="message.futurepurchase.ton"/>
		</td>
		
		<td width="20" class="titulocolumna" align="center">
			&nbsp;
		</td>
	</tr>
	<logic:notEmpty name="LIST_CUSTOMER_PURCHASE_PRODUCTS">
		<logic:iterate id="row" name="LIST_CUSTOMER_PURCHASE_PRODUCTS" indexId="int_i">
			<tr class="<%=((int_i.intValue()%2)==0?"columnadatoPar":"columnadatoNon")%>">																	
				<td align="center">&nbsp;</td>
				<td align="center"><bean:write name="row" property="productId"/></td>
				<td align="center"><bean:write name="row" property="description"/></td>
				<td align="center"><bean:write name="row" property="ton"/></td>											
				<td align="center"><a href="#>" onclick="if( confirmDelete('<bean:write name="row" property="productId"/>') )leaveThisForm( FuturePurchaseFormDeleteProduct, '<bean:write name="row" property="productId"/>');"><img alt="<bean:message key="common.delete"/>  <bean:write name="row" property="productId"/>" src="images/eliminar.jpg" border="0"></a></td>							
			</tr>
		</logic:iterate>
	</logic:notEmpty>						
</table>
<br />

	<table align="center" width="400" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
		<tr>
			<td style="text-align: center; vertical-align: bottom;" class="columnadatopar">
			<html:submit styleClass="boton"><bean:message key="label.common.save"/></html:submit>
				&nbsp;&nbsp;&nbsp;&nbsp;<html:button styleClass="boton" property="backButton" onclick="location.href='showCustomerFuturePurchaseQuery.do';"><bean:message key="label.common.cancel"/></html:button>
			</td>
		</tr>
	</table>		

</html:form>
<script>
	cleanNumber(document.getElementById('futurePurchaseByCustomerView.customerCode'));
</script>